<template>
  <div class="system-user-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <div class="system-user-search mb15">
        <el-input placeholder="请输入商品名称" size="default" style="max-width: 180px"></el-input>
        <el-button v-loading="tableData.isLoading" class="ml10" size="default" type="primary" @click="tableData.fetchData()">
          <el-icon>
            <ele-Search/>
          </el-icon>
          查询
        </el-button>
        <el-button class="ml10" size="default" type="success" @click="onOpenAdd('add',{id: undefined ,title: ''})">
          <el-icon>
            <ele-FolderAdd/>
          </el-icon>
          新增商品
        </el-button>
      </div>
      <el-tabs v-model="whereData.tabType" class="demo-tabs" @tab-change="tableData.fetchData()">
        <el-tab-pane label="出售中的商品" name="all"/>
        <el-tab-pane label="已售罄的商品" name="second"/>
        <el-tab-pane disabled label="禁戒库存商品" name="third"/>
        <el-tab-pane label="回收站的商品" name="del"/>
      </el-tabs>
      <el-table v-loading="tableData.isLoading" :data="tableData.data" style="width: 100%">
        <!--                <el-table-column type="expand">-->
        <!--                    <template #default="props">-->
        <!--                        <div m="4">-->
        <!--                            <p m="t-0 b-2">State: {{ props.row.state }}</p>-->
        <!--                            <p m="t-0 b-2">City: {{ props.row.city }}</p>-->
        <!--                            <p m="t-0 b-2">Address: {{ props.row.address }}</p>-->
        <!--                            <p m="t-0 b-2">Zip: {{ props.row.zip }}</p>-->
        <!--                            -->
        <!--                        </div>-->
        <!--                    </template>-->
        <!--                </el-table-column>-->
        <el-table-column label="商品ID" prop="id" width="60"/>
        <el-table-column label="商品图片" prop="url" show-overflow-tooltip>
          <template #default="scope">
            <el-image :src="scope.row.url" style="width: 36px;height: 36px"/>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="title" show-overflow-tooltip></el-table-column>
        <el-table-column label="商品类型" prop="type" show-overflow-tooltip>
          <template #default="scope">
            <el-tag v-if="scope.row.type === 1" size="small">普通商品</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="商品售价" prop="price" show-overflow-tooltip></el-table-column>
        <el-table-column label="销量" prop="sales" show-overflow-tooltip></el-table-column>
        <el-table-column label="库存" prop="stock" show-overflow-tooltip></el-table-column>
        <el-table-column label="排序" prop="sort" show-overflow-tooltip></el-table-column>
        <el-table-column :formatter="formatTime" label="创建时间" prop="create_time" show-overflow-tooltip></el-table-column>
        <el-table-column label="状态" prop="sort" show-overflow-tooltip>
          <template #default="scope">
            <el-switch v-if="scope.row.status == 0 || scope.row.status == 1" v-model="scope.row.status" :active-value="1" :inactive-value="0" active-text="上架" inactive-text="下架" inline-prompt size="small"/>
            <el-tag v-else-if="scope.row.status == 2" size="small" type="danger">下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenAdd('copy', scope.row)">复制</el-button>
            <el-button size="small" text type="primary" @click="onOpenAdd('edit', scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="tableData.pagination.current"
          v-model:page-size="tableData.pagination.pageSize"
          :page-sizes="tableData.pagination.pageList"
          :pager-count="5"
          :total="tableData.pagination.total"
          background
          class="mt15"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="tableData.handleSizeChange"
          @current-change="tableData.handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <UserDialog ref="userDialogRef" @refresh="tableData.fetchData()"/>
  </div>
</template>

<script lang="ts" name="systemUser" setup>
import {defineAsyncComponent, onMounted, reactive, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import usePagination from "/@/utils/usePagination"
import {formatTime} from "/@/utils/formatTime"
import {useRouter} from "vue-router"
import {useBaseApi} from "/@/api/base"

// 引入组件
const UserDialog = defineAsyncComponent(() => import('/@/views/goods/list/dialog.vue'))

// 定义变量内容
const userDialogRef = ref()
const whereData = reactive({
  tabType: 'all'
})
const tableData = usePagination({
  path: 'goods',
  page: undefined,
  size: undefined
}, whereData)
const router = useRouter()
const tabType = (e) => {
  tableData.fetchData()
}
// 打开修改用户弹窗
const onOpenAdd = (type: string, row: { title: string; id: any; }) => {
  let tagsViewName = ''
  if (type == 'add') {
    tagsViewName = '添加商品'
  } else if (type == 'copy') {
    tagsViewName = '复制:' + row.title
  } else {
    tagsViewName = '编辑:' + row.title
  }
  router.push({
    name: 'goodsAdd',
    query: {
      id: row.id,
      tagsViewName: tagsViewName,
      type: type
    },
  })
}
// 删除用户
const onRowDel = (row: { title: any; id: string | number; }) => {
  ElMessageBox.confirm(`此操作将商品：“${row.title}放入回收站”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    useBaseApi().del('goods', row.id).then(res => {

      tableData.fetchData()
      ElMessage.success('删除成功')

    })
  }).catch(() => {
  })
}
// 页面加载时
onMounted(() => {
  tableData.fetchData()
})
</script>

<style lang="scss" scoped>
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
